<?



/*

// URL
$url = $route . '/' . $section;

// Flash messages
$alerts = json_decode($session->getFlash(), true);

// Order status
$state = json_decode($_COOKIE['list-' . $section], true);








*/

// Instance the model
${$object} = new Model($options);


// Actions
switch ($action) {


	
	// List
    case 'list':
			
		// Save params for this list
		// setcookie('list-' . $section, json_encode($params), time() + 60*60*24*30, '/');
		$session->set('list-' . $section, $params);
		
		// Get the items
		$items_list = ${$object}->get($params);
		
		$columns = $items_list['info']['columns'];
		$key = $items_list['info']['key'];
		$paging = $items_list['paging'];
		$filters = $items_list['info']['filters'];
		$order = $items_list['info']['order'];

		// Actions
		$acl['add']    = ($all_access || $acl['add'])? $url . '/edit/' : false;
		$acl['detail'] = ($all_access || $acl['detail'])? $url . '/detail/id:': false;
		// Store API-like answer for debug
		$api = $items_list;
		break;


		
	// Detail
    case 'detail':
			
		// Order
		$state = $session->get('list-' . $section);
			
		// Get the item
		$id = current($id);
		$params = array('id' => $id, 'action' => 'detail', 'order' => $state['order']);
		$items = ${$object}->get($params);
		
		$paging = $items['paging'];
		$columns = $items['info']['columns'];
		$key = $items['info']['key'];
		$item = current($items['data']);
		// Actions
		$acl['update'] = ($all_access || $acl['update'])? $route . '/' . $object . '/edit/id:' . $id : false;
		$acl['delete'] = ($all_access || $acl['delete'])? $route . '/' . $object . '/delete/id:' . $id : false;
		// Back URL
		$back = $url . '/list/' . $state['url']['back'];
		// Store API-like answer for debug
		$api = $items;
		break;


		
	// Edit
    case 'edit':
		// Form for updating items
		if (current($id)) {
			$selected = ${$object}->get(array('id' => $id, 'action' => 'edit'));
			$form = 'update';
			$columns = $selected['info']['columns'];
			// Store API-like answer for debug
			$api = $selected;
		}
		// Form for adding items
		else {
			$form = 'add';
			$selected['data'][0] = array();
			$columns = ${$object}->columns('edit', $object);
			$columns = $columns['fieldset'];
			// Store API-like answer for debug
			$api = $columns;
		}
		$key = ${$object}->options['key'];
		break;


		
	// Add
    case 'add':
			
		// Order
		$state = $session->get('list-' . $section);			
			
		// Order the received data
		foreach($_REQUEST['data'] as $name => $field) {
			foreach ($field as $key => $value) {
				$selected[$key][$name] = $value;
			}
		}
		// Add each one
		foreach ($selected as $id => $data) {
			$results[] = ${$object}->add($data);
		}
		// Save results on session
		$session->setFlash(json_encode($results));
		// Redirect
		$back = $url . 'list/' . $state['url']['back'];
		header("Location: $back");
		break;


		
	// Update
    case 'update':
			
		// Order
		$state = $session->get('list-' . $section);	
			
		// Order the received data
		foreach($_REQUEST['data'] as $name => $field) {
			foreach ($field as $key => $value) {
				$selected[$key][$name] = $value;
			}
		}
		// Update each one
		foreach ($selected as $id => $data) {
			if ($id)
				$results[] = ${$object}->update($id, $data);
		}
		// Save results on session
		$session->setFlash(json_encode($results));
		// Redirect
		$back = $url . 'list/' . $state['url']['back'];
		header("Location: $back");
		break;


		
	// Delete
    case 'delete':
			
		// Order
		$state = $session->get('list-' . $section);	
			
		// Delete the item
		if ($id)
			foreach ($id as $value)
				$results[] = ${$object}->remove($value);
		// Save results on session
		$session->setFlash(json_encode($results));
		// Redirect
		$back = $url . '/list/' . $state['url']['back'];
		header("Location: $back");
		break;
		
		
		
	// Search
	case 'search':
		if ($search) {
			$url = $route . '/' . $object . '/list/';
			foreach($search as $key => $value) {
				$value = addslashes($value);
				if ($value != '') {
					$url .=  $key . ':' . $value . '/';
				}
			}
		}
		header("Location: $url");
		break;
		
}


// Render
include('themes/' . $theme . '/header.tpl');
include('themes/' . $theme . '/items.tpl');
include('themes/' . $theme . '/footer.tpl');

?>